package com.iotechn.unimall.admin.api.news;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.NewsDO;
import com.iotechn.unimall.data.mapper.NewsMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminNewsServiceImpl implements AdminNewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public Page<NewsDO> list(Integer page, Integer limit, String title, String categoryId,Integer type, Long adminId) throws ServiceException {
        List<NewsDO> NewsDOS = newsMapper.selectByList(title, categoryId, type,(page - 1) * limit, limit);
        Long count = newsMapper.countOrder(title, categoryId,type);
        return new Page<>(NewsDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NewsDO create(NewsDO newsDO, Long adminId) throws ServiceException {
        if (newsMapper.insert(newsDO) > 0) {
            newsDO.setId(newsDO.getId());
            return newsDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(NewsDO newsDO, Long adminId) throws ServiceException {
        Long id = newsDO.getId();
        if (id == null) return "fail";
        if (newsMapper.updateById(newsDO) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public NewsDO edit(String id, Long adminId) throws ServiceException {
        NewsDO newsDO = newsMapper.selectById(id);
        if (newsDO != null) return newsDO;
        throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (newsMapper.deleteById(id) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        if (newsMapper.delete(new EntityWrapper<NewsDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        return "ok";
    }

}
